package bs.app.Main;

import java.io.IOException;

import bs.app.HTTPConnection.CConnection;
import bs.app.android.DisplayJ2ME;
import bs.app.android.MIDlet;
import bs.app.android.RecordStore;

public class CMidlet extends MIDlet
{
	// Instan Canvas
	public CCanvas instanCanvas; 
	// Instan Display
	public DisplayJ2ME display;
	//	Instan Midlet
	public static CMidlet instanMidlet;
	
	public CMidlet()
	{
		System.out.println("khoi dong tao midlet");
		display= DisplayJ2ME.getDisplay(this);
		instanCanvas=new CCanvas();
		instanMidlet=this;
		loadRMS();
		try{
			CConnection.Version = this.getAppProperty("MIDlet-Version");
		}catch (Exception e) {

		}
	}
	protected void destroyApp(boolean arg0){
         /*   try {
               // if(CCanvas.s_Instan.connect.receiveSMS!=null)CCanvas.s_Instan.connect.receiveSMS.Stop();
                CCanvas.s_Instan.connect.setURL("?ctrl=ListGift&bandwidth=" + CCanvas.s_Instan.connect.bandwidth);
                CCanvas.s_Instan.connect.getViaHttpsConnection(CCanvas.s_Instan.connect.url);
            } catch (IOException e) {
            }*/
		
	}

	protected void pauseApp() {
		
	}

	protected void startApp(){
		display.setCurrent(instanCanvas);
	}
	public void exit()
	{
		notifyDestroyed();
	}
	
	final static String NAME_RECORD ="MYINFO";
	public static void loadRMS() {
		RecordStore rec = null;
		try {
			rec = RecordStore.openRecordStore(NAME_RECORD, false);
			//System.out.println("rec.getNumRecords()="+rec.getNumRecords());
			if(rec.getNumRecords() >=1){
				CConnection.sKeyUserAgent = new String(rec.getRecord(1));
			}			
			rec.closeRecordStore();
		} catch (Exception e) {
			CConnection.sKeyUserAgent ="";
		}		
	}
	/**
	 * save to RMS
	 * @param filename
	 * @param rc
	 */
	public static void saveRMS(String str) {
		try {
			RecordStore rec = RecordStore.openRecordStore(NAME_RECORD, true);
			byte data[]=str.getBytes();
			rec.addRecord(data, 0, data.length);			
			rec.closeRecordStore();
		} catch (Exception e) {
		}
	}
}


